package com.zejson.dao;


import com.zejson.dao.inf.BaseMapper;
import com.zejson.dao.inf.GeneralDaoInf;
import com.zejson.util.Mybatis3Utils;
import com.zejson.util.Page;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class GeneralDao implements GeneralDaoInf {

    private BaseMapper getExactMapper(String mapperName) throws ClassNotFoundException {
        SqlSession sqlSession = Mybatis3Utils.getSqlSession();
        return (BaseMapper) sqlSession.getMapper(Class.forName("com.zejson.dao.inf." + mapperName));
        /*
        switch (className) {
            case "RoleMapper":
            case "UserMapper":
            case "CarriersMapper":
            case "DriverMapper":
            case "GoodsMapper":
            case "LogdicMapper":
            case "SchedulingMapper":
            case "SyslogMapper":
            case "TruckMapper":
            case "TruckDriverMapper":
        }
        */
    }

    @Override
    public long countByExample(String mapperName, Object example) throws ClassNotFoundException {

        SqlSession sqlSession = Mybatis3Utils.getSqlSession();
        BaseMapper mapper = getExactMapper(mapperName);

        assert mapper != null;

        long res = mapper.countByExample(example);
        Mybatis3Utils.closeSqlSession();
        return res;
    }

    @Override
    public int deleteByExample(String mapperName, Object example) throws ClassNotFoundException {
        SqlSession sqlSession = Mybatis3Utils.getSqlSession();
        BaseMapper mapper = getExactMapper(mapperName);
        int res = mapper.deleteByExample(example);
        sqlSession.commit();
        Mybatis3Utils.closeSqlSession();
        return res;
    }

    @Override
    public int deleteByPrimaryKey(String mapperName, Integer id) throws ClassNotFoundException {
        SqlSession sqlSession = Mybatis3Utils.getSqlSession();
        BaseMapper mapper = getExactMapper(mapperName);
        int res = mapper.deleteByPrimaryKey(id);
        sqlSession.commit();
        Mybatis3Utils.closeSqlSession();
        return res;
    }

    @Override
    public int insert(String mapperName, Object record) throws ClassNotFoundException {
        SqlSession sqlSession = Mybatis3Utils.getSqlSession();
        BaseMapper mapper = getExactMapper(mapperName);
        int res = mapper.insert(record);
        sqlSession.commit();
        Mybatis3Utils.closeSqlSession();
        return res;
    }

    @Override
    public int insertSelective(String mapperName, Object record) throws ClassNotFoundException {
        SqlSession sqlSession = Mybatis3Utils.getSqlSession();
        BaseMapper mapper = getExactMapper(mapperName);
        int res = mapper.insertSelective(record);
        sqlSession.commit();
        Mybatis3Utils.closeSqlSession();
        return res;
    }

    @Override
    public List<Object> selectByExample(String mapperName, Object example) throws ClassNotFoundException {
        SqlSession sqlSession = Mybatis3Utils.getSqlSession();
        BaseMapper mapper = getExactMapper(mapperName);
        List<Object> res = mapper.selectByExample(example);
        Mybatis3Utils.closeSqlSession();
        return res;
    }

    @Override
    public Object selectByPrimaryKey(String mapperName, Integer id) throws ClassNotFoundException {
        SqlSession sqlSession = Mybatis3Utils.getSqlSession();
        BaseMapper mapper = getExactMapper(mapperName);
        Object res = mapper.selectByPrimaryKey(id);
        Mybatis3Utils.closeSqlSession();
        return res;
    }

    @Override
    public int updateByExampleSelective(String mapperName, Object record, Object example) throws ClassNotFoundException {
        SqlSession sqlSession = Mybatis3Utils.getSqlSession();
        BaseMapper mapper = getExactMapper(mapperName);
        int res = mapper.updateByExampleSelective(record, example);
        sqlSession.commit();
        Mybatis3Utils.closeSqlSession();
        return res;
    }

    @Override
    public int updateByExample(String mapperName, Object record, Object example) throws ClassNotFoundException {
        SqlSession sqlSession = Mybatis3Utils.getSqlSession();
        BaseMapper mapper = getExactMapper(mapperName);
        int res = mapper.updateByExample(record, example);
        sqlSession.commit();
        Mybatis3Utils.closeSqlSession();
        return res;
    }

    @Override
    public int updateByPrimaryKeySelective(String mapperName, Object record) throws ClassNotFoundException {
        SqlSession sqlSession = Mybatis3Utils.getSqlSession();
        BaseMapper mapper = getExactMapper(mapperName);
        int res = mapper.updateByPrimaryKeySelective(record);
        sqlSession.commit();
        Mybatis3Utils.closeSqlSession();
        return res;
    }

    @Override
    public int updateByPrimaryKey(String mapperName, Object record) throws ClassNotFoundException {
        SqlSession sqlSession = Mybatis3Utils.getSqlSession();
        BaseMapper mapper = getExactMapper(mapperName);
        int res = mapper.updateByPrimaryKey(record);
        sqlSession.commit();
        Mybatis3Utils.closeSqlSession();
        return res;
    }

    @Override
    public void setPage(String mapperName, Page page) throws ClassNotFoundException {
        SqlSession sqlSession = Mybatis3Utils.getSqlSession();
        BaseMapper mapper = getExactMapper(mapperName);
        List<Object> res = mapper.getByLimit((page.getPageNum() - 1) * page.getDataSize(), page.getPageNum() * page.getDataSize());
        sqlSession.commit();
        Mybatis3Utils.closeSqlSession();
        page.setData(res);
    }

    @Override
    public int insertByAutoGeneratePK(String mapperName, Object record) throws ClassNotFoundException {
        SqlSession sqlSession = Mybatis3Utils.getSqlSession();
        BaseMapper mapper = getExactMapper(mapperName);
        int res = mapper.insertByAutoGeneratePK(record);
        sqlSession.commit();
        Mybatis3Utils.closeSqlSession();
        return res;
    }

}
